package com.crazy.base.starter.springdoc.configuration;


import com.crazy.base.starter.springdoc.properties.SpringdocProperties;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
@RequiredArgsConstructor
public class SpringdocAutoConfiguration {

    private final SpringdocProperties springdocProperties;

    @Bean
    @ConditionalOnMissingBean
    public SpringdocProperties swaggerProperties() {
        return new SpringdocProperties();
    }

    @Bean
    @ConditionalOnMissingBean
    public OpenAPI springOpenApi() {
        return new OpenAPI()
                .info(new Info()
                        .title(springdocProperties.getTitle())
                        .description(springdocProperties.getDescription())
                        .version(springdocProperties.getVersion())
                        .contact(new Contact().url(springdocProperties.getExternalDocs().getUrl()))
                        .license(new License()
                                .name(springdocProperties.getLicense())
                                .url(springdocProperties.getLicenseUrl())
                        )
                )
                .externalDocs(new ExternalDocumentation()
                        .description(springdocProperties.getExternalDocs().getDescription())
                        .url(springdocProperties.getExternalDocs().getUrl()));
    }

    @Bean
    @ConditionalOnMissingBean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group(springdocProperties.getGroup())
                .pathsToMatch("/**")
                .build();
    }
}
